package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.web.controller.system.utils.JointInvestigation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController {

    @Autowired
    ISysDeptService sysDeptService;

    @GetMapping("/roleDeptTreeselect/{roleId}")
    public AjaxResult getRoleByDept(@PathVariable Long roleId){
        List<SysDept> depts = sysDeptService.selectDeptList(new SysDept());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", sysDeptService.selectDeptListByRoleId(roleId));
        List<SysDept> men = JointInvestigation.getMen(depts);
        List<TreeSelect> collect = men.stream().map(TreeSelect::new).collect(Collectors.toList());
        ajax.put("depts", collect);
        return ajax;
    }



    @GetMapping("/list")//主页面
    public AjaxResult selectall(SysDept sysDept){//查看状态
        List<SysDept> getadd = sysDeptService.selectDeptList(sysDept);
        return AjaxResult.success(gjl.getadd(getadd));
    }

    @PostMapping//新增
    public Integer jk(@Validated @RequestBody SysDept sysDept){//新增
       sysDept.setCreateBy(getUsername());
        int i = sysDeptService.insertDept(sysDept);
        return i;
    }

    @DeleteMapping("/{deptId}")//删除
    public Integer as(@PathVariable Long deptId){
        int delete = sysDeptService.delete(deptId);
        return delete;
    }
    @GetMapping("/{deptId}")//單查
    public AjaxResult selea(@PathVariable Long deptId){
        return AjaxResult.success(sysDeptService.selectalldancha(deptId));
    }

    @GetMapping("/list/exclude/{deptId}")
    public AjaxResult seleaa(@PathVariable Long deptId){
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        return AjaxResult.success(gjl.getadd(sysDepts));
    }
    @PutMapping//修改
    public Integer mm(@Validated @RequestBody SysDept sysDept){
        sysDept.setUpdateBy(getUsername());
        int i = sysDeptService.updateDept(sysDept);
        return i;
    }
    @GetMapping("/treeselect")
    public AjaxResult treeselect(SysDept sysDept){
        List<SysDept> sysDeptList = sysDeptService.selectDeptList(sysDept);
        return AjaxResult.success(sysDeptService.buildDeptTreeSelect(sysDeptList));
    }
}
